﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Hxsoft.AppV1;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.Admin.RoleManage
{
	public partial class Default : BasePage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (Roles.GetManageListRight())
			{
				if (!IsPostBack)
				{
					LoadList();
					Page.DataBind();
				}
			}
			else
			{
				Messages.Add(new PageMessage(false, null, PageMessageType.Error, Res.PermissionExceptionMessageTitle, Res.PermissionExceptionMessage));
			}
		}

		protected void pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
		{
			pager.CurrentPageIndex = e.NewPageIndex;
			LoadList();
		}

		protected void BtSearch_Click(object sender, EventArgs e)
		{
			pager.CurrentPageIndex = 1;
			LoadList();
		}

		protected void BtReload_Click(object sender, EventArgs e)
		{
			LoadList();
		}

		protected void List_ItemCommand(object source, RepeaterCommandEventArgs e)
		{
			Guid identifier = GetListItemIdentifier(e.Item);
			string messages = "";

			switch (e.CommandName)
			{
				case "Delete":
					messages = Delete(identifier);
					break;
			}

			if (!String.IsNullOrEmpty(messages))
			{
				Messages.Add(new PageMessage(true, null, PageMessageType.Error, Res.OperationErrorTitle, messages));
			}

			LoadList();
		}

		protected void List_ItemCreated(object sender, RepeaterItemEventArgs e)
		{

		}

		protected void List_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				Guid identifier = GetListItemIdentifier(e.Item);
				HyperLink HlAccredit = (HyperLink)e.Item.FindControl("HlAccredit");
				LinkButton LbtDelete = (LinkButton)e.Item.FindControl("LbtDelete");

				Role role = Roles.GetItem(identifier);

				bool disallowUserAccredit = Roles.RolePropertyHasProperty(role.PropertyCode, Roles.RolePropertyCode.DisallowUserAccredit);
				bool disallowRoleAccredit = Roles.RolePropertyHasProperty(role.PropertyCode, Roles.RolePropertyCode.DisallowRoleAccredit);
				if (disallowUserAccredit && disallowRoleAccredit)
				{
					HlAccredit.Enabled = false;
				}
				else
				{
					HlAccredit.Enabled = true;
					if (!disallowUserAccredit)
						HlAccredit.Attributes["onclick"] = @"javascript:OpenWindowCenter(""AddUsersToRole.aspx?RoleId=" + identifier + @"&SiteId=" + SiteId + @""");";
					else
						HlAccredit.Attributes["onclick"] = @"javascript:OpenWindowCenter(""Accredit.aspx?RoleId=" + identifier + @"&SiteId=" + SiteId + @""");";
				}

				bool disallowDelete = Roles.RolePropertyHasProperty(role.PropertyCode, Roles.RolePropertyCode.DisallowDelete);
				if (disallowDelete)
				{
					LbtDelete.Enabled = false;
				}
				else
				{
					LbtDelete.Enabled = true;
				}
			}
		}

		/// <summary>
		/// 加载列表。
		/// </summary>
		protected void LoadList()
		{
			PaginationInfo<List<Role>> pageInfo =
				Roles.GetPaginationManageList(SiteId, TbKeyword.Text, pager.PageSize, pager.CurrentPageIndex);

			pager.RecordCount = pageInfo.RecordCount;

			List.DataSource = pageInfo.Records;
			List.DataBind();

			pager.CustomInfoHTML = Helper.GetPagerCustomInfoHtml<List<Role>>(pageInfo);
		}

		/// <summary>
		/// 取得 ListItem 的标识符。
		/// </summary>
		/// <param name="item">列表的 ListItem。</param>
		/// <returns>ListItem 的标识符。</returns>
		protected Guid GetListItemIdentifier(RepeaterItem item)
		{
			Guid identifier;
			HiddenField control;

			control = (HiddenField)item.FindControl("Identifier_RoleId");
			identifier = new Guid(control.Value);

			return identifier;
		}

		/// <summary>
		/// 取得 ListItem 的站点名称。
		/// </summary>
		/// <param name="item">列表的 ListItem。</param>
		/// <returns> ListItem 的站点名称。</returns>
		protected string GetListItemSiteName(RepeaterItem item)
		{
			int? siteId = ((Role)item.DataItem).SiteId;
			if (siteId.HasValue)
				return Sites.GetSiteName(siteId.Value);
			return null;
		}

		/// <summary>
		/// 取得 ListItem 的站点名称。
		/// </summary>
		/// <param name="item">列表的 ListItem。</param>
		/// <returns> ListItem 的站点名称。</returns>
		protected string GetListItemCreatorName(RepeaterItem item)
		{
			Guid? creatorId = ((Role)item.DataItem).CreatorId;
			if (creatorId.HasValue)
				return Users.GetUserName(creatorId.Value);
			return null;
		}

		/// <summary>
		/// 删除指定的信息
		/// </summary>
		/// <param name="identifier">要删除信息的标识符。</param>
		/// <returns>如果删除发生错误则返回错误消息。</returns>
		protected string Delete(Guid identifier)
		{
			if (Roles.GetDeleteRight(Roles.GetItem(identifier)))
			{
				Roles.Delete(identifier);
				return null;
			}
			else
				return string.Format(Res.PermissionException_DeleteAppointedObject, identifier);
		}
	}
}
